package com.booking.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booking.common.dto.PageReq;
import com.booking.common.dto.PageResp;
import com.booking.common.utils.ConvertUtils;
import com.booking.controller.model.req.analyse.AnalyseQueryReq;
import com.booking.controller.model.req.order.OrderPageQueryReq;
import com.booking.dao.entity.ServiceOrder;
import com.booking.dao.mapper.ServiceOrderMapper;
import com.booking.dao.service.ServiceOrderService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Qikai.Lin
 * @date 2023/4/23
 */
@Service
public class ServiceOrderServiceImpl extends ServiceImpl<ServiceOrderMapper, ServiceOrder>
        implements ServiceOrderService {
    @Override
    public PageResp<ServiceOrder> page(PageReq<OrderPageQueryReq> request) {
        OrderPageQueryReq condition = request.getCondition();
        QueryWrapper<ServiceOrder> pageQueryWrapper = new QueryWrapper<>();
        pageQueryWrapper.eq(condition.getUserId() != null, "visitor_id", condition.getUserId());
        pageQueryWrapper.eq(condition.getHomestayId() != null, "homestay_id", condition.getHomestayId());
        pageQueryWrapper.eq(Strings.isNotEmpty(condition.getOrderStatus()), "order_status", condition.getOrderStatus());
        Page<ServiceOrder> orderPage = new Page<>(request.getCurrent(), request.getSize());
        Page<ServiceOrder> page = this.page(orderPage, pageQueryWrapper);
        return ConvertUtils.convert(PageResp.class, page);
    }

    @Override
    public List<ServiceOrder> getListByServiceId(Long serviceId) {
        QueryWrapper<ServiceOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("service_id", serviceId);
        return this.list(queryWrapper);
    }

    @Override
    public List<ServiceOrder> getListByVisitorId(Long visitorId) {
        QueryWrapper<ServiceOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("visitor_id", visitorId);
        return this.list(queryWrapper);
    }

    @Override
    public List<ServiceOrder> getListByCondition(AnalyseQueryReq request) {
        QueryWrapper<ServiceOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(request.getHomestayId() != null , "homestay_id", request.getHomestayId());
        queryWrapper.eq(request.getServiceId() != null , "service_id", request.getServiceId());
        return this.list(queryWrapper);
    }
}
